package com.ym.ganio.article;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.ym.ganio.R;
import com.ym.ganio.bean.ArticleItem;
import com.ym.ganio.define.ReadDefine;

/**
 * Created by yumodev on 17/6/6.
 */

public class ArticleFragment extends Fragment {
    private WebView mWebView;
    private ArticleItem mArticleData;
    private Toolbar mToolbar;
    private LinearLayout mTitleGroupView;
    private ProgressBar mProgressBar;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mArticleData = getArguments().getParcelable(ReadDefine.PARAME_KEY_ARTICLE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup view = (ViewGroup)inflater.inflate(R.layout.article_page, null, false);
        mTitleGroupView = (LinearLayout) view.findViewById(R.id.title_group);
        initToolbar();
        initProgressBar();
        setupView(view);
        return view;
    }

    private void initToolbar(){
        mToolbar = (Toolbar)mTitleGroupView.findViewById(R.id.toolbar);
        mToolbar.setTitle(mArticleData.getDesc());
        mToolbar.setTitleTextColor(getResources().getColor(R.color.common_text_color));
        mToolbar.setNavigationIcon(R.drawable.icon_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
    }

    private void initProgressBar(){
        //mProgressBar = new ProgressBar(getContext(),null, android.R.attr.progressBarStyleHorizontal);
        mProgressBar = (ProgressBar)mTitleGroupView.findViewById(R.id.progressbar);
        int progressBarHeight = getContext().getResources().getDimensionPixelOffset(R.dimen.progress_bar);
        mProgressBar.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, progressBarHeight));
    }

    private void setupView(ViewGroup view){
        if (mWebView == null){
            mWebView = new WebView(getContext());
            view.addView(mWebView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }

        mWebView.loadUrl(mArticleData.getUrl());
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return false;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                mProgressBar.setProgress(0);
                mProgressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mProgressBar.setVisibility(View.GONE);
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                mProgressBar.setProgress(newProgress);
            }
        });
    }
}
